package com.siam.package_user.feign;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.siam.package_common.constant.Quantity;
import com.siam.package_common.entity.BasicResult;
import com.siam.package_common.exception.StoneCustomerException;
import com.siam.package_user.entity.Member;
import com.siam.package_user.entity.MemberTradeRecord;
import com.siam.package_user.entity.MemberWithdrawRecord;
import com.siam.package_user.service.MemberService;
import com.siam.package_user.service.MemberTradeRecordService;
import com.siam.package_user.service.MemberWithdrawRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 提供给其他服务Feign客户端调用的用户模块相关接口
 */
@RestController
public class MemberWithdrawRecordFeignProvider implements MemberWithdrawRecordFeignApi {

    @Autowired
    private MemberWithdrawRecordService memberWithdrawRecordService;

    @Autowired
    private MemberTradeRecordService memberTradeRecordService;

    @Autowired
    private MemberService memberService;

    public BasicResult autoPayment(){
        memberWithdrawRecordService.autoPayment();
        return BasicResult.success();
    }

    @Override
    public BasicResult updateAuditStatusSuccess(String orderNo) {
        // 查询符合条件的记录
        LambdaQueryWrapper<MemberWithdrawRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MemberWithdrawRecord::getOrderNo, orderNo);
        queryWrapper.eq(MemberWithdrawRecord::getAuditStatus, MemberWithdrawRecord.AUDIT_STATUS_PAYMENT_PROCESSING);
        MemberWithdrawRecord dbMemberWithdrawRecord = memberWithdrawRecordService.getOne(queryWrapper);
        if(dbMemberWithdrawRecord == null){
            throw new StoneCustomerException("[修改用户提现记录状态]该记录查询为空,orderNo=" + orderNo);
        }

        // 修改审核状态
        MemberWithdrawRecord updateMemberWithdrawRecord = new MemberWithdrawRecord();
        updateMemberWithdrawRecord.setId(dbMemberWithdrawRecord.getId());
        updateMemberWithdrawRecord.setAuditStatus(MemberWithdrawRecord.AUDIT_STATUS_SUCCESS);
        updateMemberWithdrawRecord.setAuditTime(new Date());
        updateMemberWithdrawRecord.setUpdateTime(new Date());
        memberWithdrawRecordService.updateById(updateMemberWithdrawRecord);

        //添加用户交易记录
        MemberTradeRecord insertMemberTradeRecord = new MemberTradeRecord();
        insertMemberTradeRecord.setMemberId(dbMemberWithdrawRecord.getMemberId());
        insertMemberTradeRecord.setOutTradeNo(orderNo);
        insertMemberTradeRecord.setType(Quantity.INT_5);
        insertMemberTradeRecord.setPaymentMode(Quantity.INT_1);
        insertMemberTradeRecord.setAmount(dbMemberWithdrawRecord.getWithdrawAmount());
        insertMemberTradeRecord.setStatus(Quantity.INT_2);
        insertMemberTradeRecord.setCreateTime(new Date());
        insertMemberTradeRecord.setUpdateTime(new Date());
        memberTradeRecordService.save(insertMemberTradeRecord);

        //TODO-发消息通知用户
        return BasicResult.success();
    }

    @Override
    public BasicResult updateAuditStatusFail(String orderNo, String auditReason) {
        // 查询符合条件的记录
        LambdaQueryWrapper<MemberWithdrawRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MemberWithdrawRecord::getOrderNo, orderNo);
        queryWrapper.eq(MemberWithdrawRecord::getAuditStatus, MemberWithdrawRecord.AUDIT_STATUS_PAYMENT_PROCESSING);
        MemberWithdrawRecord dbMemberWithdrawRecord = memberWithdrawRecordService.getOne(queryWrapper);
        if(dbMemberWithdrawRecord == null){
            throw new StoneCustomerException("[修改用户提现记录状态]该记录查询为空,orderNo=" + orderNo);
        }

        // 修改审核状态
        MemberWithdrawRecord updateMemberWithdrawRecord = new MemberWithdrawRecord();
        updateMemberWithdrawRecord.setId(dbMemberWithdrawRecord.getId());
        updateMemberWithdrawRecord.setAuditStatus(MemberWithdrawRecord.AUDIT_STATUS_FAIL);
        updateMemberWithdrawRecord.setAuditReason(auditReason);
        updateMemberWithdrawRecord.setAuditTime(new Date());
        updateMemberWithdrawRecord.setUpdateTime(new Date());
        memberWithdrawRecordService.updateById(updateMemberWithdrawRecord);

        //添加用户交易记录
        MemberTradeRecord insertMemberTradeRecord = new MemberTradeRecord();
        insertMemberTradeRecord.setMemberId(dbMemberWithdrawRecord.getMemberId());
        insertMemberTradeRecord.setOutTradeNo(orderNo);
        insertMemberTradeRecord.setType(Quantity.INT_5);
        insertMemberTradeRecord.setPaymentMode(Quantity.INT_1);
        insertMemberTradeRecord.setAmount(dbMemberWithdrawRecord.getWithdrawAmount());
        insertMemberTradeRecord.setStatus(Quantity.INT_3);
        insertMemberTradeRecord.setCreateTime(new Date());
        insertMemberTradeRecord.setUpdateTime(new Date());
        memberTradeRecordService.save(insertMemberTradeRecord);

        Member dbMember = memberService.selectByPrimaryKey(dbMemberWithdrawRecord.getMemberId());

        // 提现失败后置处理
        memberWithdrawRecordService.handleWithdrawFail(dbMemberWithdrawRecord, dbMember);

        return BasicResult.success();
    }
}